package org.gitos.cloud.comet.tcp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by shizhengchu on 15/12/24
 * 将channelHandlerContext保存在map中
 */
public class RegistCtxHandler extends ChannelInboundHandlerAdapter {
    private static final Logger log = LoggerFactory.getLogger(RegistCtxHandler.class);

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        ConnectionHolder.CHANNEL_HANDLER_CONTEXT_MAP.remove(ctx.channel().id().asLongText());
        super.channelUnregistered(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        String channelId = ctx.channel().id().asLongText();
        log.info("channelId:{}", channelId);
        ConnectionHolder.CHANNEL_HANDLER_CONTEXT_MAP.put(channelId, ctx);
        super.channelRegistered(ctx);
    }
}
